<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/4/1
 * Time: 14:24
 */

namespace App\Extensions\Tools;

use App\Patent;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class PatentMonitorBatch extends BatchAction
{
    protected $selector = '.monitor-posts';

    public function handle(Collection $collection, Request $request)
    {
        $is_monitor = $request->get('is_monitor');
        if (!in_array($is_monitor, [1, 2])) {
            $this->response()->status = false;
            return $this->response()->swal()->success('选选择监控状态');
        }
        foreach ($collection as $item) {
            if ($item instanceof Patent) {
                $item->is_monitor = $is_monitor;
                $item->save();
            }
        }
        return $this->response()->swal()->success('操作成功!')->refresh();
    }

    public function form()
    {
        $this->radio('is_monitor', '监控状态')->options([1 => '加入年费监控', 2 => '放弃年费监控'])->required();
    }

    public function html()
    {
        return "<a class='monitor-posts btn btn-sm btn-danger'><i class='fa fa-hand-pointer-o'></i>年费监控</a>";
    }

    public function authorize($user, Collection $collection)
    {
        return $user->role_type == 1;
    }
}
